home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOD2TXT.ZIP / CHAP10.TXT < prev    next >
Text File  |  1987-03-25  |  13KB  |  322 lines

  1.                  Chapter 10 - Scalars, subranges, and Sets
  2.  
  3.  
  4.                       PREREQUISITES FOR THIS MATERIAL
  5.  
  6.              In  order  to understand the material in this  chapter,
  7.         you should have a fairly good understanding of the  material
  8.         in Part I of this tutorial.
  9.  
  10.              A scalar,  also called an enumerated type, is a list of
  11.         values  which a variable of that type may assume.   Look  at
  12.         the  file named ENTYPES.MOD for an example of some  scalars.
  13.         The  first  TYPE declaration defines "Days" as being a  type
  14.         which can take on any one of seven  values.   Since,  within
  15.         the  VAR declaration,  "Day" is assigned the type of "Days",
  16.         then  "Day" is a variable which can assume any one of  seven
  17.         different values.  Moreover, "Day" can be assigned the value
  18.         "mon",  or "tue",  etc.,  which makes the program easier  to
  19.         follow and understand.  Internally, the Modula-2 system does
  20.         not  actually assign the value "mon" to the variable  "Day",
  21.         but it uses an integer representation for each of the names.
  22.         This  is  important to understand because you  must  realize
  23.         that you cannot print out "mon",  "tue",  etc., but can only
  24.         use them for indexing control statements.
  25.  
  26.              Note  that  there is an upper limit  of  16  enumerated
  27.         types  placed  on you by most implementations  of  Modula-2.
  28.         This  is  actually a very low limit and is most  unfortunate
  29.         that this limit exists.
  30.  
  31.              The   second  line  of  the  type  definition   defines
  32.         "TimeOfDay" as another "type".  The variable "Time" can only
  33.         be  assigned  one of four values since it is defined as  the
  34.         type  "TimeOfDay".   It should be clear that even though  it
  35.         can be "morning", it cannot be assigned "morningtime" or any
  36.         other  variant  spelling  of morning,  since  it  is  simply
  37.         another  identifier which must have an exact spelling to  be
  38.         understood by the compiler.
  39.  
  40.              Several  REAL  variables  are defined to  allow  us  to
  41.         demonstrate the use of the scalar variables.   After writing
  42.         a header for our output,  the REAL variables are initialized
  43.         to some values that are probably not real life  values,  but
  44.         will serve to illustrate use of the scalar variable.
  45.  
  46.  
  47.                          A BIG SCALAR VARIABLE LOOP
  48.  
  49.              The  remainder  of the program is one large loop  being
  50.         controlled  by the variable "Day" as it goes through all  of
  51.         its values,  one at a time.   Note that the loop could  have
  52.         gone  from  "tue" to "sat" or whatever portion of the  range
  53.         desired.   It does not have to go through all of the  values
  54.         of "Day".  Using "Day" as the CASE variable, the name of one
  55.  
  56.  
  57.                                 Page 60
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  Chapter 10 - Scalars, subranges, and Sets
  68.  
  69.  
  70.         of  the  days  of the week is written out each  time  we  go
  71.         through  the  loop.   Another loop controlled by  "Time"  is
  72.         executed four times, once for each value of "Time".  The two
  73.         CASE  statements within the inner loop are used to calculate
  74.         the total pay rate for each time period and each  day.   The
  75.         data  is formatted carefully to make a nice looking table of
  76.         pay rates as a function of "Time" and "Day".
  77.  
  78.              Take  careful  notice  of  the  fact  that  the  scalar
  79.         variables never entered into the calculations, and they were
  80.         not printed out.  They were only used to control the flow of
  81.         the logic.   It was much neater than trying to remember that
  82.         "mon"  is represented by a 0,  "tue" is represented by a  1,
  83.         etc.   In  fact,  those  numbers are used for  the  internal
  84.         representation of the scalars,  but we can relax and let the
  85.         Modula-2  system worry about the internal representation  of
  86.         our scalars.
  87.  
  88.              Compile  and run this program and observe the  form  of
  89.         the  output  data.   The  only format  available  with  some
  90.         compilers  are  the "E" notation which does not make  for  a
  91.         very  well formatted or easily read output.   Don't let this
  92.         worry you,  when we get to Part III of this tutorial we will
  93.         see how we can write our own output routines to display,  or
  94.         print, floating point numbers in any format we can think up.
  95.  
  96.              One  other thing should be pointed out in this  module.
  97.         If you observe the CASE statements you will notice that  the
  98.         one that starts in line 33 does not have an ELSE clause.  It
  99.         is  really not needed because every possible value that  the
  100.         variable  "Day" can have is covered in the various branches.
  101.         In the CASE statement starting in line 51,  there is an ELSE
  102.         clause  because only two of the possible 7 values are  acted
  103.         on in the CASE body itself.   Without the ELSE,  the program
  104.         would  not  know what to do with a value  of  "mon"  through
  105.         "fri",  so the ELSE is required here, but not in the earlier
  106.         one.
  107.  
  108.  
  109.                         LETS LOOK AT SOME SUBRANGES
  110.  
  111.              Examine  the  program SUBRANGE.MOD for  an  example  of
  112.         subranges.   It  may  be expedient to define some  variables
  113.         that  only  cover a part of the full range as defined  in  a
  114.         scalar type.   Notice that "Days" is declared a scalar  type
  115.         as  in the last program,  and "Work" is declared a type with
  116.         an  even  more restricted range.   In the  VAR  declaration,
  117.         "Day" is once again defined as the days of the week and  can
  118.         be  assigned any of the days by the program.   The  variable
  119.         "Workday",  however,  is  assigned the type "Work",  and can
  120.         only  be  assigned  the days "mon"  through  "fri".   If  an
  121.  
  122.  
  123.                                 Page 61
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  Chapter 10 - Scalars, subranges, and Sets
  134.  
  135.  
  136.         attempt  is  made to assign "Workday"  the  value  "sat",  a
  137.         runtime  error  will  be  generated.   A  carefully  written
  138.         program  will  never  attempt  that,  and  it  would  be  an
  139.         indication  that something is wrong with either the  program
  140.         or the data.  This is one of the advantages of Modula-2 over
  141.         older languages.
  142.  
  143.              Further   examination  will  reveal  that  "Index"   is
  144.         declared  as  being  capable of storing only  the  range  of
  145.         INTEGERS from 1 to 12.   During execution of the program, if
  146.         an  attempt  is made to assign "Index" any value outside  of
  147.         that range,  a runtime error will be generated.  Suppose the
  148.         variable  "Index" was intended to refer to  your  employees,
  149.         and  you have only 12.   If an attempt was made to refer  to
  150.         employee number 27,  or employee number -8, there is clearly
  151.         an  error  somewhere in the data and you would want to  stop
  152.         running the payroll to fix the problem.  Modula-2 would have
  153.         saved you a lot of grief.
  154.  
  155.                     SOME STATEMENTS WITH ERRORS IN THEM
  156.  
  157.              In  order to have a program that would compile  without
  158.         errors,  and  yet show some errors,  the first part  of  the
  159.         program  is  not  really a part of the program since  it  is
  160.         within a comment area.  This is a trick to remember when you
  161.         are debugging a program, a troublesome part can be commented
  162.         out  until you are ready to include it with the  rest.   The
  163.         errors are self explanatory.
  164.  
  165.              Going  beyond the area commented out,  there are  seven
  166.         assignment statements as examples of subrange variable  use.
  167.         Notice  that  the variable "Day" can always be assigned  the
  168.         value of either "Workday" or "Weekend",  but the reverse  is
  169.         not  true  because  "Day" can assume values  that  would  be
  170.         illegal for the other variables.
  171.  
  172.  
  173.                         THREE VERY USEFUL FUNCTIONS
  174.  
  175.              The  last  section of the example program  demonstrates
  176.         the  use  of  three  very  important  functions  when  using
  177.         scalars.   The first is the "INC" function which returns the
  178.         value  of  the  scalar following that  scalar  used  as  the
  179.         argument.   If the argument is the last value in the list, a
  180.         runtime error is generated.   The next function is the "DEC"
  181.         that  returns the value of the prior scalar to that used  in
  182.         the  argument.   All scalars have an internal representation
  183.         starting  at  0  and  increasing by one  until  the  end  is
  184.         reached.   The  third  function is the  "ORD"  which  simply
  185.         returns the numerical value of the scalar variable.
  186.  
  187.  
  188.  
  189.                                 Page 62
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                  Chapter 10 - Scalars, subranges, and Sets
  200.  
  201.  
  202.              In our example program, ORD(Day) is 5 if "Day" has been
  203.         assigned "sat",  but ORD(Weekend) is 0 if "Weekend" has been
  204.         assigned "sat".   As you gain experience in programming with
  205.         scalars  and subranges,  you will realize the value of these
  206.         three functions.
  207.  
  208.              A few more thoughts about subranges are in order before
  209.         we go on to another topic.   A subrange is always defined by
  210.         two  predefined  constants,  and  is always  defined  in  an
  211.         ascending  order.   A variable defined as a subrange type is
  212.         actually  a  variable defined with a restricted  range,  and
  213.         should  be  used as often as possible in  order  to  prevent
  214.         garbage  data.   There are actually very few variables  ever
  215.         used  that cannot be restricted by some amount.   The limits
  216.         may give a hint at what the program is doing and can help in
  217.         understanding  the program operation.   Subrange  types  can
  218.         only be constructed using the simple data types.
  219.  
  220.  
  221.                                     SETS
  222.  
  223.              Now  for  a new topic,  sets.   Examining  the  example
  224.         program  SETS.MOD  will reveal use of some sets.   A  scalar
  225.         type  is defined first,  in this case the scalar type  named
  226.         "Goodies".   A  set is then defined with the reserved  words
  227.         SET  OF  followed  by  a  predefined  scalar   type.    Most
  228.         microcomputers  have an upper limit of 16 elements that  can
  229.         be used in a set.
  230.  
  231.              Several variables are defined as sets of "Treat", after
  232.         which  they  can  individually be assigned portions  of  the
  233.         entire set.  Consider the variable "IceCreamCone" which  has
  234.         been  defined  as a set of type "Treat".   This variable  is
  235.         composed  of  as many elements of "Goodies" as  we  care  to
  236.         assign  to  it.   In  the program,  we define  it  as  being
  237.         composed of "IceCream",  and "Cone".  The set "IceCreamCone"
  238.         is  therefore  composed  of  two elements,  and  it  has  no
  239.         numerical or alphabetic value as most other variables  have.
  240.         Continuing  in  the program,  you will see 4 more  delicious
  241.         deserts  defined as sets of their components.   Notice  that
  242.         the banana split is first defined as a range of terms,  then
  243.         another term is added to the group.   All five are  combined
  244.         in  the set named "Mixed",  then "Mixed" is subtracted  from
  245.         the entire set of values to form the set of ingredients that
  246.         are not used in any of the deserts.  Each ingredient is then
  247.         checked  to  see if it is IN the set of unused  ingredients,
  248.         and printed out if it is.  Running the program will reveal a
  249.         list of the unused elements.
  250.  
  251.              In this example, better programming practice would have
  252.         dictated   defining   a  new   variable,   possibly   called
  253.  
  254.  
  255.                                 Page 63
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                  Chapter 10 - Scalars, subranges, and Sets
  266.  
  267.  
  268.         "Remaining"  for the ingredients that were unused.   It  was
  269.         desirable  to  illustrate that "Mixed" could be  assigned  a
  270.         value  based on subtracting itself from the entire  set,  so
  271.         the poor variable name was used.
  272.  
  273.              This  example  resulted in some  nonsense  results  but
  274.         hopefully it led your thinking toward the fact that sets can
  275.         be  used for inventory control,  possibly a parts allocation
  276.         scheme, or some other useful system.
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                 Page 64
  322.